using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Microsoft.Practices.CompositeUI.SmartParts;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace NetRosApp.Infrastructure.Interface.Base.BaseViews
{
    public partial class BaseTraziViewTest : XtraUserControl, IBaseTraziView, ISmartPartInfoProvider
    {
        bool NotModal = true;

        ItemClickEventHandler _onIzvjestaj;
        ItemClickEventHandler _onNovi;
        ItemClickEventHandler _onTrazi;
        ItemClickEventHandler _onClear;
        ItemClickEventHandler _onUgasi;
        String _title = "Genericki trazi";
        String _description = "Genericki trazi Description";


        [Category("SmartPartInfo")]
        public String Title
        {
            set { _title = value; }
            get { return _title; }
        }

        [Category("SmartPartInfo")]
        public String Description
        {
            set { _description = value; }
            get { return _description; }
        }

        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnIzvjestaj
        {
            add
            {
                _onIzvjestaj += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                _onIzvjestaj -= value;
                barButtonItem1.ItemClick -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnNovi
        {
            add
            {
                _onNovi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem2.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem2.ItemClick -= value;
                _onNovi -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnTrazi
        {
            add
            {
                _onTrazi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem3.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem3.ItemClick -= value;
                _onTrazi -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnClear
        {
            add
            {
                _onClear += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem4.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem4.ItemClick -= value;
                _onClear -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnUgasi
        {
            add
            {
                _onUgasi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem5.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem5.ItemClick -= value;
                _onUgasi -= value;
            }
        }




        public BaseTraziViewTest()
        {
            InitializeComponent();
        }



        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x100;
            const int WM_SYSKEYDOWN = 0x104;

            if (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.F5:
                        if (NotModal & _onIzvjestaj != null)
                        {
                            _onIzvjestaj.Invoke(this, new ItemClickEventArgs(barButtonItem1, null));
                        }
                        break;
                    case Keys.F6:
                        if (_onNovi != null)
                        {
                            _onNovi.Invoke(this, new ItemClickEventArgs(barButtonItem2, null));
                        }
                        break;
                    case Keys.F8:
                        if (_onTrazi != null)
                        {
                            _onTrazi.Invoke(this, new ItemClickEventArgs(barButtonItem3, null));
                        }
                        break;

                    case Keys.F9:
                        if (_onClear != null)
                        {
                            _onClear.Invoke(this, new ItemClickEventArgs(barButtonItem4, null));
                        }
                        break;

                    case Keys.F12:
                        if (_onUgasi != null)
                        {
                            _onUgasi.Invoke(this, new ItemClickEventArgs(barButtonItem5, null));
                        }
                        break;


                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected internal void CloseAndReleaseView(IBaseTraziPresenter _presenter)
        {
            _presenter.OnCloseView();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if ((Parent as Form) != null)
            {
                NotModal = false;
                if ((Parent as Form) != null)
                {
                    barButtonItem1.Enabled = false;
                    this.Dock = DockStyle.Fill;
                }
            }
        }

         public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
        {
            ISmartPartInfo result = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
            result.Title = _title;
            result.Description = _description;
            return result;
        }
        public object SmartPartView
        {
            get { return this; }
        }

     public bool IsFocused()
        {
            return this.ContainsFocus;
        }

        protected internal void OpenEntityEditor(IBaseTraziPresenter _presenter,object Entity,Type EditorClass)
        {
            if (Entity != null)
            {

                if (!NotModal)
                {

                    _presenter.OpenEditorNewOrEditinParent(Entity);
                    try
                    {
                        (Parent as Form).Close();
                    }
                    catch { }
                }
                else
                {
                    _presenter.OpenEditorNewOrEditinTab(Entity, EditorClass);
                }
            }
        }

        protected internal void OpenEntityEditorNew(IBaseTraziPresenter _presenter, Type EditorClass)
        {
            if (!NotModal)
            {
                _presenter.OpenEditorNewModal(EditorClass);
            }
            else
            {
                _presenter.OpenEditorNewOrEditinTab(null, EditorClass);
            }

        }

        public void GridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                OpenEditor();
            }

        }

        public void GridControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.GridControl tmp = sender as DevExpress.XtraGrid.GridControl;
            GridHitInfo hi = tmp.FocusedView.CalcHitInfo(new System.Drawing.Point(e.X, e.Y)) as GridHitInfo;
            if (hi.InRow)
            {
                OpenEditor();
            }

        }

        protected virtual void OpenEditor()
        {
            MessageBox.Show("OpenEditor");
        }
    }
}
